/** 
 * Project Name:tinysql 
 * File Name:PlusArgumentUtil.java 
 * Package Name:com.wdy.golden.tinysql.plus.util 
 * Date:2021年4月26日下午9:23:33 
 * 
*/  
package com.wdy.golden.tinysql.plus.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.wdy.golden.tinysql.plus.annotation.TinyBuilderOperation;
import com.wdy.golden.tinysql.plus.annotation.TinyCommonOperation;
import com.wdy.golden.tinysql.plus.builder.ITinySqlBuilder;
import com.wdy.golden.tinysql.plus.builder.SqlBuilder;
import com.wdy.golden.tinysql.plus.builder.TinySqlBuilderMethod;
import com.wdy.golden.tinysql.plus.common.ITinysqlPlus;
import com.wdy.golden.tinysql.plus.common.TinysqlPlusMethod;
import com.wdy.golden.tinysql.util.StringUtil;

/** 
 * ClassName:PlusArgumentUtil <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason:   TODO ADD REASON. <br/> 
 * Date:     2021年4月26日 下午9:23:33 <br/> 
 * @author   DongYe_Wu 
 * @version
 * @see       
 */
public class PlusArgumentUtil {
	public static Map<String, Object> processArguments(Class<?> interfaceType, Method method, Object[] args) {
		Map<String, Object> argMap = new HashMap<String, Object>();
		
		if(method.isAnnotationPresent(TinyCommonOperation.class)) {
			TinyCommonOperation tinyCommonOperation = method.getAnnotation(TinyCommonOperation.class);
			//
			Class<?> clazz = PlusClassUtil.getInterfaceGenericClass(interfaceType, ITinysqlPlus.class);
			if(StringUtil.equals(tinyCommonOperation.name(), TinysqlPlusMethod.tinyInsert.name())) {
				argMap.putAll(PlusClassUtil.getEntityMap(clazz.getDeclaredFields(), args[0]));
			}else if(
					StringUtil.equals(tinyCommonOperation.name(), TinysqlPlusMethod.tinyDeleteByIds.name())
					||
					StringUtil.equals(tinyCommonOperation.name(), TinysqlPlusMethod.tinyDeleteById.name())
					||
					StringUtil.equals(tinyCommonOperation.name(), TinysqlPlusMethod.tinySelectById.name())
					||
					StringUtil.equals(tinyCommonOperation.name(), TinysqlPlusMethod.tinySelectByIds.name())
			){
				argMap.put(PlusClassUtil.getIdFieldName(clazz), args[0]);
			}else if(
					StringUtil.equals(tinyCommonOperation.name(), TinysqlPlusMethod.tinyUpdateById.name())
					||
					StringUtil.equals(tinyCommonOperation.name(), TinysqlPlusMethod.tinyUpdateByIds.name())
			) {
				argMap.putAll(PlusClassUtil.getEntityMap(clazz.getDeclaredFields(), args[0]));
				argMap.put(PlusClassUtil.getIdFieldName(clazz), args[1]);
			}
		}else if(method.isAnnotationPresent(TinyBuilderOperation.class)) {
			TinyBuilderOperation tinyBuilderOperation = method.getAnnotation(TinyBuilderOperation.class);
			//
			SqlBuilder<?> builder = (SqlBuilder<?>)args[0];
			Class<?> clazz = PlusClassUtil.getInterfaceGenericClass(interfaceType, ITinySqlBuilder.class);
			if(StringUtil.equals(tinyBuilderOperation.name(), TinySqlBuilderMethod.selectPage.name())) {
				argMap.putAll(PlusClassUtil.getEntityMap(clazz.getFields(), builder.getEntity()));
			}else {
				argMap.putAll(PlusClassUtil.getEntityMap(clazz.getDeclaredFields(), builder.getEntity()));
			}
		}
		
		return argMap;
	}
}
